#pragma once
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <iostream>
#include <string>

class InetAddr
{
public:
    InetAddr(const struct sockaddr_in& local)
        :_addr(local)
    {
        // _ip = inet_ntoa(_addr.sin_addr); 
        // _ip.resize(32, 0);  //这个有问题 ，所以根本就不能用这个
        // _ip.reserve(32); 

        inet_ntop(AF_INET, &_addr.sin_addr.s_addr, &_ip[0], _ip.size());
        std::cout << "ip: " << _ip << std::endl;

        // char buffer[32];
        // inet_ntop(AF_INET, &_addr.sin_addr.s_addr, buffer, sizeof buffer);
        // _ip = buffer;
        _port = ntohs(_addr.sin_port);
        std::cout << "port: " << _port << std::endl;
    }
    std::string IP()
    {
        return _ip;
    }
    uint16_t Port()
    {
        return _port;
    }
    const struct sockaddr_in& GetAddr()
    {
        return _addr;
    }
    bool operator==(const InetAddr& addr)
    {
        return this->_ip == addr._ip && this->_port == addr._port; //加上端口，本地测试
    }
    std::string PrintDebug()
    {
        std::string str;
        str += "[";
        str += _ip;
        str += ":";
        str += std::to_string(_port);
        str += "]# ";
        // for(int i = 0; i < str.size(); i++)
        //     std::cout << i << ": "<<str[i] << std::endl;  //Debug 终于找到了
        std::cout << str << str.size()<< std::endl; //debug
        return str;
    }
private:
    std::string _ip;
    uint16_t _port;
    struct sockaddr_in _addr;
};